package com.whales.lms.rms.scheduler.task;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

import com.whales.lms.rms.scheduler.TaskTemplate;
import com.whales.lms.rms.scheduler.service.IFService;
import com.whales.lms.rms.web.model.FaultVO;
import com.whales.lms.rms.web.model.ScheduleVO;
import com.whales.lms.rms.web.service.FaultService;
import com.whales.lms.rms.web.service.ScheduleService;
import com.whales.lms.rms.web.util.ConstantUtil;

@Component
public class SystemAliveCheckTask extends TaskTemplate  {
	@Inject
	private ScheduleService scheduleService;
	@Inject
	private FaultService faultService;
	@Inject
	private IFService ifService;
	
	public SystemAliveCheckTask() {
		this.setConExpression("0 0/5 * * * ?"); //5분 간격
	}
	
	@Override
	public void execute() throws Exception {
		for (ScheduleVO toLivePc : scheduleService.getToLivePcListByTime()) {
			String result = ConstantUtil.STATE_OKE;
			if(ConstantUtil.STATE_OKE.equals(ifService.ping(toLivePc.getIp()))){
				if(!ConstantUtil.STATE_OKE.equals(ifService.agentAlive(toLivePc.getIp()))){
					result = ConstantUtil.STATE_HAE;
				}
			}else{
				result = ConstantUtil.STATE_HNE;
			}
			//OKE든 아니든 무조건 insert 한다.
			FaultVO faultVO = new FaultVO();
			faultVO.setIp(toLivePc.getIp());
			faultVO.setSepType(ConstantUtil.TYPE_HW);
			faultVO.setFaultCode(result);
			faultService.create(faultVO);
		}
	}
}
